package com.ruoyi.contract.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.domain.entity.SysDictType;
import com.ruoyi.contract.domain.ConBusinessType;
import com.ruoyi.contract.service.IConBusinessTypeService;
import com.ruoyi.options.domain.Option;

import com.ruoyi.system.service.ISysDictDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/contract/businessType")
public class ConBusinessTypeController extends BaseController {

    @Autowired
    IConBusinessTypeService conBusinessTypeService;

    @Autowired
    ISysDictDataService sysDictDataService;

    @GetMapping("/label")
    public AjaxResult label(Long conId){

        List<Option> options = new ArrayList<>();
//        System.out.println("11111111"+conId);

        List<ConBusinessType> businessTypes = conBusinessTypeService.list(new QueryWrapper<ConBusinessType>().lambda().eq(ConBusinessType::getConId,conId));

        SysDictData dictType = new SysDictData();

        dictType.setDictType("project_type");

        List<SysDictData> dictDataList = sysDictDataService.selectDictDataList(dictType);

        if(businessTypes.size()>0){

            for(ConBusinessType businessType:businessTypes){

                Option option = new Option();

                option.setValue(businessType.getConBussinessType());

                option.setKey(businessType.getConBussinessType());

                option.setLabel(dictDataList.stream().filter(dictData -> dictData.getDictValue().equals(businessType.getConBussinessType())).findFirst().get().getDictLabel());

                options.add(option);
            }

        }

        options = options.stream().distinct().collect(Collectors.toList());

        return AjaxResult.success(options);

    }

}
